import { getSelectDataMapList, getTenantID, removeDateHmsStr, renderTableText, tableSorter } from '@/utils';
import { Tooltip, TreeSelect, Input } from 'antd';
import pageStyles from '@/common/pageLayout.less';
import { DictionaryRenderFun, formatDecimal, formatDecimalFour } from '../../Acommon';
import { ILoadMapTypeAll } from '@/common';
import { CopyOutlined } from '@ant-design/icons';
// Table表格的配置
const getTableColumns = ({ batchSyncData, changeLocation, changeLocationInfo, loadMapInfo, mapLoading }) => {
  const assetAreaList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_AREA}`], 'name', 'id', true);

  return [
    {
      title: '资产编号',
      dataIndex: 'assetCode',
      key: 'assetCode',
      width: 160,
      fixed: 'left',
      visible: true,
      sorter: (a: any, b: any) => (a.assetCode > b.assetCode ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '资产名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 160,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '资产分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClassName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 170,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '状态',
    //   dataIndex: 'assetStateName',
    //   key: 'assetStateName',
    //   width: 120,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetStateName'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    // {
    //   title: '所属仓库',
    //   dataIndex: 'assetStoreName',
    //   key: 'assetStoreName',
    //   width: 150,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetStoreName'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'quantityOrArea'),
      render: (text: number | null) => formatDecimal(text),
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 140,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurementName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '资产原值（元）',
      dataIndex: 'totalValue',
      key: 'totalValue',
      width: 170,
      visible: true,
      sorter: (a: any, b: any) => (a.totalValue > b.totalValue ? 1 : -1),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '使用部门',
      dataIndex: 'useDepartmentName',
      key: 'useDepartmentName',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a.useDepartmentName > b.useDepartmentName ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '使用人',
      dataIndex: 'userName',
      key: 'userName',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a.userName > b.userName ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: () => {
        return (
          <>
            <span style={{ color: '#f00', fontSize: '18px' }}></span> 存放区域
          </>
        );
      },
      dataIndex: 'locationName',
      key: 'locationName',
      width: 150,
      visible: true,
      fixed: 'right',
      sorter: (a: any, b: any) => (a.locationName > b.locationName ? 1 : -1),
      // render: (text: string) => renderTableText(text, true),
      render: (text: string, record: Record<string, any>) => {
        return (
          <TreeSelect
            suffixIcon={
              <>
                <Tooltip title="批量同步存放区域数据">
                  <a>
                    <CopyOutlined onClick={() => batchSyncData('locationName', record)} />
                  </a>
                </Tooltip>
              </>
            }
            status={record?.selected && !text ? 'error' : ''}
            bordered={record?.selected && !text ? true : false}
            loading={mapLoading}
            placeholder="请选择"
            treeData={assetAreaList}
            fieldNames={{
              label: 'name',
              value: 'id',
              children: 'children',
            }}
            value={text}
            style={{ width: '100%' }}
            showSearch={true}
            treeNodeFilterProp={'name'}
            allowClear={false}
            onChange={(value: string) => {
              changeLocation(record.id, value);
            }}
          />
        );
      },
    },
    {
      title: '详细地点',
      dataIndex: 'locationInfo',
      key: 'locationInfo',
      width: 180,
      visible: true,
      fixed: 'right',
      sorter: (a: any, b: any) => (a.locationInfo > b.locationInfo ? 1 : -1),
      // render: (text: string) => renderTableText(text, true),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Input.Group compact>
            <Tooltip title="批量同步详细地点数据">
              <a onClick={() => batchSyncData('locationInfo', record)}>
                <CopyOutlined style={{ position: 'absolute', right: 15, bottom: 8, zIndex: 2 }} />
              </a>
            </Tooltip>
            <Input.TextArea
              value={text}
              status={record?.selected && !text ? 'error' : ''}
              bordered={record?.selected && !text ? true : false}
              rows={1}
              cols={1}
              placeholder="请输入"
              onChange={(e) => {
                changeLocationInfo(record.id, e.target.value);
              }}
              style={{ width: '100%' }}
            />
          </Input.Group>
        );
      },
    },
  ];
};

export { getTableColumns };
